package test;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class ServletDemo extends HttpServlet {
    public ServletDemo() {
        System.out.println("ServletDemo 被创建了");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String servletName = config.getServletName();
        System.out.println(servletName + "的初始化参数如下:");
        Enumeration<String> names = config.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = config.getInitParameter(name); // 根据名称获取参数
            System.out.println(name + "=>" + value);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-type", "text/html;charset=utf-8");
        resp.getWriter().println("<h1>hello servlet demo</h1>");
    }
}
